XfMissionChapterExecute={
	private ["_chapter_succeeded","_olaf_freed","_olaf_alive","_hostage_task_updated","_last_random_reinforcement","_random_reinf_diff","_reinforcement_power"];
	diag_log "";
	diag_log "=========================================================================";
	diag_log "                             CHAPTER 2                                   ";
	diag_log "=========================================================================";
	diag_log "";
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter2.sqf","XfMissionChapterExecute","Starting Chapter"];
	DM_Mission_Chapter_CanRun = True;
	DM_Mission_Chapter_Runs = True;
	DM_Mission_Chapter_Success = 0;
	
	call XfMissionChapterSetup;
	_chapter_succeeded = false;
	if (!DM_Mission_Mission_CanRun) exitWith {diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter2.sqf","XfMissionChapterSetup","Mission canceled!"];};
	_hostage_task_updated = false;
	_kill_task_updated = false;	
	_start_enemy_counts = ([DM_Chapter_Position,500,d_enemy_side] call XfMissionGetZoneSideCount) select 0;
	_enemy_diff = (_start_enemy_counts / 3);
	_player_detected_notification = false;
	while {DM_Mission_Chapter_CanRun AND DM_Mission_Mission_CanRun} do {
		sleep 10;
		_enemy_counts = ([DM_Chapter_Position,500,d_enemy_side] call XfMissionGetZoneSideCount) select 0;
		//TODO: Find why it won't exit the loop...
		_olaf_freed = ["jip_olaf_freed",false] call XNetGetJIP;
		_olaf_alive = ["jip_olaf_alive",false] call XNetGetJIP;
		_player_is_detected = ["jip_player_detected",false] call XNetGetJIP;
		if (!isNil "_olaf_freed") then {
			if (_olaf_freed and !_hostage_task_updated) then {
				["FindOlafAndFree","", "","",[],"Succeeded"] call XfMissionTask;
				_hostage_task_updated = true;
				sleep 10;
				"Olaf sent us the train position. He also sent the position of the Radio Tower used by the enemy to communicate with their HQ." call XfMissionMessageToAll;
			};
		} else {
			_olaf_freed = false;
		};
		if (_olaf_freed) then {
			_chapter_succeeded = _olaf_alive;
			DM_Mission_Chapter_CanRun = false;
		} else {
			if (!isNil "_olaf_alive") then {
				// If Olaf dies, it'll automatically stop the chapter and mark it as failed.
				_chapter_succeeded = _olaf_alive;
				DM_Mission_Chapter_CanRun = _olaf_alive;
			};
			// Sending random reinforcement if configured in the mission editor
		};
		// This IF contains additional checks for special events or actions which should only occur if the mission is still active.
		If (DM_Mission_Chapter_CanRun) then {
			If (_player_is_detected && !_player_detected_notification) then {
				"You are detected! The enemy will send reinforcement!" call XfMissionMessageToAll;
				_player_detected_notification = true;
			};
			If (_player_is_detected and ((_start_enemy_counts - _enemy_counts) > _enemy_diff)) then {
				[d_enemy_side,[DM_Chapter_Position,100],DM_Mission_RandomReinforcement,"RANDOM",True] call XfMissionRandomReinforcement;
				diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Random reinforcement sent!"];
				hint "Reinforcement";
				// We raise the number of enemies which need to be killed to slow down reinforcement over time and eventually stop it.
				_enemy_diff = _enemy_diff + 2;
			};
		};
	};
	sleep 30;
	if (DM_Mission_Mission_CanRun) then {
		if (_chapter_succeeded) then {
			call XfMissionChapterSuccess;
			sleep 15;
			["Ending Chapter 2",date] call XfMissionStartScreen;
			sleep 15;
		} else {
			call XfMissionChapterFailure;
		};
	} else {
		diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter2.sqf","XfMissionChapterSetup","Mission canceled!"];
	};
	call XfMissionChapterCleanUp;
	DM_Mission_Chapter_Runs = False;
};

XfMissionChapterSetup={
	private ["_town_Kamyshovo","_town_Kamyshovo_pos","_town_Kamyshovo_size","_mission_starter","_grp","_grpSpecOps1","_enemy_power","_enemy_coeff"];
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter2.sqf","XfMissionChapterSetup","Starting Initialization"];
	_town_Kamyshovo = d_town_markers select 23;
	_town_Kamyshovo_pos = _town_Kamyshovo select 0;
	_town_Kamyshovo_size = _town_Kamyshovo select 2;
	DM_Chapter_Position = _town_Kamyshovo_pos;
	// This indicates where the mission takes place
	[_town_Kamyshovo_pos,_town_Kamyshovo_size,_town_Kamyshovo select 1] call XfMissionCurrentPosition;
	// Position somewhat to the north of Kamyshovo, in the woods
	[[12120.1,4198.94,0],160] call XfMissionPlaceStartTeleporter;
	// We create the hostage
	["jip_olaf_freed",false] call XNetSetJIP;
	_grp = ["CIV"] call x_creategroup;
	_pos_olaf = [12006.7,3466.22,-0.112923];
	_building_pos_hiding = [_town_Kamyshovo_pos,_town_Kamyshovo_size,"HIDE"] call XfMissionBuildingAreaPos;
	if (count _building_pos_hiding > 0) then {_pos_olaf = _building_pos_hiding select 0};
	["Functionary2",_grp,_pos_olaf,"RESCUE",["You freed Olaf, congratulations!","You did your best...","jip_olaf_freed","jip_olaf_alive"],"doStop this;"] call XfMissionCreateMissionUnit;
	// We create the task
	["FindOlafAndFree","Find Olaf and free him", "You need to find Olaf in the town of Kamyshovo. From what we know, the enemy forces are going to arrest him and transfer him to their camp where he'll be tortured. We need to avoid that at all cost! Find him, free him and exchange informations for his money. Beware as they moved there in force!","",[],"Created"] call XfMissionTask;
	// We create the Mission markers on the map.
	[false,["chapter2_start_misson", [12120.1,4198.94,0], "ICON", "ColorBlue", [1,1],"Start",160,"hd_start","",1]] call XfMissionMarker;
	[false,["chapter2_Kamyshovo_position", _town_Kamyshovo_pos, "ICON", "ColorRed", [1,1],"Olaf",0,"hd_objective","",1]] call XfMissionMarker;
	// We populate the town with a reasonable amount of enemies.
	[d_enemy_side,[_town_Kamyshovo_pos,_town_Kamyshovo_size],true] call XfMissionPopulateLocation;
	// We set a defensive group of specops near the house.
	_grpSpecOps1 = [d_enemy_side,[_pos_olaf,30],[],1,false] call XfMissionCreateFootmenGroup;
	
	// The trigger which will send reinforcement when the player is detected!
	["jip_player_detected",false] call XNetSetJIP;
	[_town_Kamyshovo_pos,_town_Kamyshovo_size * 1.5,"WEST","EAST","jip_player_detected"] call XfMissionSideDetected;
	
	// This will create snipers automatically if selected in the mission setup
	[d_enemy_side,[_town_Kamyshovo_pos,_town_Kamyshovo_size],DM_Mission_Snipers] call XfMissionCreateSnipersArea;
	// This will create air support automatically if selected in the mission setup
	[d_enemy_side,[_town_Kamyshovo_pos,_town_Kamyshovo_size],DM_Mission_AirSupport] call XfMissionCallAirSupport;
};

XfMissionChapterSuccess={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter2.sqf","XfMissionChapterSuccess","Starting"];
	DM_Mission_Chapter_Success = 1;
};

XfMissionChapterFailure={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter2.sqf","XfMissionChapterFailure","Starting"];
	DM_Mission_Chapter_Success = -1;
};

// Self-explanatory
XfMissionChapterCleanUp={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter2.sqf","XfMissionChapterCleanUp","Starting"];
	// Giving an empty position places the teleporter back to the hangar, closing the hangar.
	[[],0] call XfMissionPlaceStartTeleporter;
	// Giving only the marker name will trigger the deletion of the marker.
	[false,["chapter2_start_misson"]] call XfMissionMarker;
	[false,["chapter2_Kamyshovo_position"]] call XfMissionMarker;
	[] call XfMissionCleanTriggers;
	[] call XfMissionCleanObjects;
	[] call XfMissionCleanTasks;
};

DM_Mission_Chapter_Compiled = true;